<?php

/**
 * This is the model class for table "{{cmp_services}}".
 *
 * The followings are the available columns in table '{{cmp_services}}':
 * @property integer $zcs_id
 * @property integer $zm_id
 * @property integer $zcs_type
 * @property string $zcs_account
 * @property string $zcs_name
 * @property string $zcs_tel
 * @property integer $zcs_listorder
 * @property integer $zcs_default
 */
class CmpServices extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CmpServices the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cmp_services}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, zcs_type, zcs_listorder, zcs_default', 'numerical', 'integerOnly'=>true),
			array('zcs_account', 'length', 'max'=>100),
			array('zcs_name', 'length', 'max'=>50),
			array('zcs_tel', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zcs_id, zm_id, zcs_type, zcs_account, zcs_name, zcs_tel, zcs_listorder, zcs_default', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zcs_id' => 'Zcs',
			'zm_id' => 'Zm',
			'zcs_type' => 'Zcs Type',
			'zcs_account' => 'Zcs Account',
			'zcs_name' => 'Zcs Name',
			'zcs_tel' => 'Zcs Tel',
			'zcs_listorder' => 'Zcs Listorder',
			'zcs_default' => 'Zcs Default',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zcs_id',$this->zcs_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zcs_type',$this->zcs_type);
		$criteria->compare('zcs_account',$this->zcs_account,true);
		$criteria->compare('zcs_name',$this->zcs_name,true);
		$criteria->compare('zcs_tel',$this->zcs_tel,true);
		$criteria->compare('zcs_listorder',$this->zcs_listorder);
		$criteria->compare('zcs_default',$this->zcs_default);

		$criteria->order = "zcs_listorder ASC";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	//返回客服账号类型
	public function getZcsType($type=null){
		if($type==null){
			return array(
					'1'=>'QQ账号',
					'2'=>'MSN',
					'3'=>'旺旺',
			);
		}else{
			$level = $this->getZcsType();
			return $level[$type];
		}
	}
	
	/**
	 * 获取商家客服信息
	 * @param $id商家ID，为空则取当前商家，为1最后台商家，其它请写商家ID
	 * return array 最一条为默认客服
	 */
	public function getServices($id=null){
		$id = ($id==null)?Company::model()->getCmpUserId():$id;
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',$id);
		$criteria->order = "zcs_default DESC, zcs_listorder ASC";
		$result = $this->findAll($criteria);
		return $result;
	}
	//随机获取一个客服信息
	public function getServiceRand($id=null){
		$id = ($id==null)?Company::model()->getCmpUserId():$id;
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',$id);
		$criteria->order = " rand() limit 1";
		$result = $this->findAll($criteria);
		return $result;
	}
}